package com.csx.governance.sample.serviceA.controller;

import com.csx.governance.sample.serviceA.provider.ServiceProviderClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author cuisongxu
 * @date 2023/4/2 18:32
 */
@RequestMapping("/serviceA")
@RestController
public class ServiceAController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private Registration registration;

    @Autowired
    private ServiceProviderClient serviceProviderClient;

    /**
     * 这是测试普通全链路(不包含消息全链路)
     * @param request
     * @return
     */
    @GetMapping("/test")
    public String test(HttpServletRequest request) {
        System.out.println(request);
        String swimLane = request.getHeader("swimLane");
        System.out.println(swimLane);
        Map<String, String> metadata = registration.getMetadata();
        String host = registration.getHost();
        int port = registration.getPort();
        String instanceInfo = host + ":" + port + "  metadata: " + metadata.toString();
        String url = "http://providerB/providerB/test";
        String res = restTemplate.getForObject(url, String.class);
        return "serviceA " + instanceInfo + " ->  \n" + res;
    }

    @GetMapping("/test2")
    public String test2() {
        String url = "http://providerxxx:8002/providerxxx/test";
        String res = restTemplate.getForObject(url, String.class);
        return "serviceA -> " + res;
    }


    // ******************************* 测试OpenFeign ***************************************
    @GetMapping("/testFeign")
    public String testFeign(){
        Map<String, String> metadata = registration.getMetadata();
        String host = registration.getHost();
        int port = registration.getPort();
        String instanceInfo = host + ":" + port + "  metadata: " + metadata.toString();
        String res = serviceProviderClient.testFeign();
        return "serviceA openfeign " + instanceInfo + " ->  \n" + res;
    }

    /**
     * 这是测试包含消息的全链路的接口
     * @param request
     * @return
     */
    @GetMapping("/testFullLink")
    public String testFullLink(HttpServletRequest request) {
        System.out.println(request);
        String swimLane = request.getHeader("swimLane");
        System.out.println(swimLane);
        Map<String, String> metadata = registration.getMetadata();
        String host = registration.getHost();
        int port = registration.getPort();
        String instanceInfo = host + ":" + port + "  metadata: " + metadata.toString();
        String url = "http://providerB/providerB/sendMsg?msg=xxxxxmsg";
        String res = restTemplate.getForObject(url, String.class);
        return "serviceA " + instanceInfo + " ->  \n" + res;
    }


}
